<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <link href="../css/register.css" rel="stylesheet">
</head>
<body>




<div class="form-div">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有账号?</span><a href="#">登录</a>
    </div>
    <form id="reg-from" action="#" method="post">
        <table>




            <tr>
                <td>用户名</td>
                <td class="inputs">
                    <input name="username" type="text" id="username">
                    <br>
                    <span id="username_err" class="err_msg" style="display: none">用户名格式不正确</span>
                </td>
            </tr>

            <tr>
                <td>密码</td>
                <td class="inputs">
                    <input name="password" type="text" id="password">
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码格式不正确</span>
                </td>
            </tr>
            <tr>
                <td>手机号</td>
                <td class="inputs">
                    <input name="tel" type="text" id="tel">
                    <br>
                    <span id="tel_err" class="err_msg" style="display: none">手机号格式不正确</span>
                </td>
            </tr>

        </table>

        <div class="buttons">
            <input value="注 册" type="submit" id="reg_btn">
        </div>
        <br class="clear">

    </form>
</div>

<script>
    //1.验证用户名是否符合规则
    //1.1获取用户名输入框
    let usernameInput = document.getElementById("username");

    //1.2绑定onblur事件 失去焦点
    usernameInput.onblur = checkUsername;
        function  checkUsername() {
        //1.3获取用户输入的用户名
        var username=usernameInput.value.trim();

        //1.4判断用户名是否符合规则：长度 6~12位,单词字符组成
            var reg =/^\w{6,12}$/;
            let flag = reg.test(username);
            // var flag = username.length >= 6 && username.length <= 12;
        if (flag) {
            //符合规则
            document.getElementById("username_err").style.display='none';

        }else {
            //不符合规则
            document.getElementById("username_err").style.display='';
        }
        return flag;
    }

    //1.验证密码是否符合规则
    //1.1获取密码输入框
    let passwordInput = document.getElementById("password");

    //1.2绑定onblur事件 失去焦点
    passwordInput.onblur = checkPassword;
        function checkPassword() {
        //1.3获取用户输入的密码
        var password=passwordInput.value.trim();

        //1.4判断密码是否符合规则：数字 6~12位
            var reg =/^\w{6,12}$/;
            let flag = reg.test(password);
            // var flag = password.length >= 6 && password.length <= 12;
        if (flag) {
            //符合规则
            document.getElementById("password_err").style.display='none';
        }else {
            //不符合规则
            document.getElementById("password_err").style.display='';
        }
        return flag;
    }

    //1.验证手机号是否符合规则
    //1.1获取手机号输入框
    let telInput = document.getElementById("tel");

    //1.2绑定onblur事件 失去焦点
    telInput.onblur = checkTel;

        function checkTel() {
        //1.3获取用户输入的手机号
        var tel=telInput.value.trim();

        //1.4判断手机号是否符合规则：数字 6~12位,数字组成，第一位是1
            var reg =/^[1]\d{10}$/;
            let flag = reg.test(tel);

            // var flag = tel.length == 11;
        if (flag) {
            //符合规则
            document.getElementById("tel_err").style.display='none';
            return true;
        }else {
            //不符合规则
            document.getElementById("tel_err").style.display='';
        }
        return flag;
    }


    //表单的验证
    //1.1获取表单的对象
    let regfrom = document.getElementById("reg-from");

    //2.绑定事件
    regfrom.onblur=function () {
      //挨个判断是否符合要求
       var flag =checkUsername() && checkPassword() && checkTel();
       return flag;
    };


</script>













</body>
</html>